<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="com.butnet.sqltools.web.Action"%>
<%@page import="com.butnet.sqltools.connection.*"%>
<%@page import="java.sql.Connection" %>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.List"%>
<%@page import="java.sql.Connection" %>
<%@page import="java.util.Map.Entry"%>
<%@page import="net.sf.json.JSONObject"%>
<%String name = request.getParameter("name"); %>
<html>
<head>
<title>在 <%=name%> 中执行SQL</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<style type="text/css">
* {
	font-size: 12px;
}
.header {
	font-weight: bold;
	font-style: italic;
	font-size: 14px;
	background-color: #E0E0E0;
}
table tr td {
	height: 25px;
	padding-left: 7px;
	padding-right: 7px;
}
</style>
<script type="text/javascript">
function onout(dom){
	dom.style.backgroundColor='';
}
function onin(dom){
	dom.style.backgroundColor='#c0c0c0';
}
</script>
<script type="text/javascript">
var historys=<%=JSONObject.fromMap(Action.getDbTools().getHistory()).toString()%>;
var showing = false;
var showSwitch = null;
function showHistory(showAll){
	showing = !showing;
	if(showing){
		document.getElementById('showPanel').style.display='';
	}else{
		var name=document.getElementById('name').value;
		if(showAll)name='';
		if(showSwitch==name){
			document.getElementById('showPanel').style.display='none';
			return;
		}
	}
	if(!showAll){
		var name=document.getElementById('name').value;
		showSwitch = name;
		if(!historys[name]){
			alert('连接 '+name+' 没有查询历史');
			return;
		}
		var content=document.getElementById('content');
		var html='<table style="border-collapse: collapse;" borderColor="#E0E0E0" cellSpacing="0" cellPadding="0" border="1">';
		html+='<tr class="header"><td>连接名称</td><td>历史查询</td></tr>';
		for(var i=0;i<historys[name].length;i++){
			html+='<tr onmouseout="onout(this)" onmouseover="onin(this);" onclick="onclickSql(\''+name+'\','+i+')">';
			if(i==0){
				html+='<td rowspan="'+historys[name].length+'" valign="middle">'+name+'</td>';
			}
			html+='<td>'+historys[name][i]+'</td>';
			html+='</tr>';
		}
		html+='</table>';
		content.innerHTML=html;
	}else{
		showSwitch = '';
		var html='<table style="border-collapse: collapse;" borderColor="#E0E0E0" cellSpacing="0" cellPadding="0" border="1">';
		html+='<tr class="header"><td>连接名称</td><td>历史查询</td></tr>';
		for(name in historys){
			for(var i=0;i<historys[name].length;i++){
				html+='<tr onmouseout="onout(this)" onmouseover="onin(this);" onclick="onclickSql(\''+name+'\','+i+')">';
				if(i==0){
					html+='<td rowspan="'+historys[name].length+'" valign="middle">'+name+'</td>';
				}
				html+='<td>'+historys[name][i]+'</td>';
				html+='</tr>';
			}
		}
		html+='</table>';
		var content=document.getElementById('content');
		content.innerHTML=html;
	}
}
function onclickSql(name, index){
	if(historys[name]){
		document.getElementById('sql').value=historys[name][index];
	}else{
		alert('连接 '+name+' 没有查询历史');
	}
}
</script>
</head>
<body>
<form action="Action?method=bulid" method="POST">
SQL:  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="执行">&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;&nbsp;
<select name="name" id="name">
<%
for(Iterator<Entry<String, Connection>> it = Action.getDbTools().getHeader(); it.hasNext();){
	Entry en = it.next();
%>
	<option value="<%=en.getKey() %>" <%=name.equals(en.getKey())?"selected=selected":"" %>><%=en.getKey() %></option>
<%}%>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="Action?method=search">返回管理页面</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="showHistory(false);return false;">当前查询历史</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="showHistory(true);return false;">所有查询历史</a>
<br/>
<div style="display:none" id="showPanel"><div id="content"></div></div>
<textarea name="sql" id="sql" style="width:1000px;height:400px"></textarea>
</form>
</body>
</html>
